{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 日期：2023-3-24\n",
    "* 课程：api\n",
    "* 记录人：陈虹妃"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 本周内容\n",
    "1.人脸比对 2.人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1686986294,1da7f3e6-5d92-4300-bc47-028de3fae96f', 'time_used': 1434, 'confidence': 54.367, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': '7c94c7d2c88d2b438c55d96904df37eb', 'face_rectangle': {'top': 229, 'left': 100, 'width': 524, 'height': 524}}], 'faces2': [{'face_token': '3ecc1794b5b001b6df997a52eb55044b', 'face_rectangle': {'top': 1314, 'left': 214, 'width': 1233, 'height': 1233}}], 'image_id1': 'avh2N/IHA9cJcL/yj/TfXw==', 'image_id2': '/etiqZMFFuJAqP2FSK3oAg=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('IMG_7687.jpg', 'rb')\n",
    "f_02 = open('IMG_9527.JPG', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'a974e8f5e1d5594baf68d23426221932', 'time_used': 159, 'face_count': 0, 'face_added': 0, 'request_id': '1686986366,437f1a03-a070-4711-b0b7-0a6c9bac3c4d', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"display_name\":\"广州南方学院东20\"\n",
    "   \n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'display_name': '广州南方学院东20',\n",
       " 'face_count': 1,\n",
       " 'face_tokens': ['5a60d7a287c6d18df8c8815c3cc55d51'],\n",
       " 'faceset_token': '32089d204389ecd65fd26a698d8e6b01',\n",
       " 'outer_id': '',\n",
       " 'request_id': '1686986387,ce24cf6d-a925-4d77-b2aa-40220327a761',\n",
       " 'tags': '',\n",
       " 'time_used': 128,\n",
       " 'user_data': ''}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"faceset_token\":\"32089d204389ecd65fd26a698d8e6b01\"\n",
    "   \n",
    "}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# e67a093affbf0d8cac7ac86951d20013 # 7eafb713635c89740b8c9f87b91ae74e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'img' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-683fb33f0a3b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[0;34m\"api_key\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m     \u001b[0;34m\"api_secret\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m     \u001b[0;34m\"image_base64\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     19\u001b[0m }\n\u001b[1;32m     20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'img' is not defined"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：钟，陈\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('IMG_7687.jpg', 'rb')\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"image_base64\":img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'img' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-473f794c2093>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     16\u001b[0m     \u001b[0;34m\"api_key\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     17\u001b[0m     \u001b[0;34m\"api_secret\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 18\u001b[0;31m     \u001b[0;34m\"image_base64\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     19\u001b[0m }\n\u001b[1;32m     20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'img' is not defined"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测：钟、陈\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('IMG_9527.JPG', 'rb')\n",
    "\n",
    "\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"image_base64\":img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'face_added': 0,\n",
       " 'face_count': 1,\n",
       " 'faceset_token': '32089d204389ecd65fd26a698d8e6b01',\n",
       " 'failure_detail': [],\n",
       " 'outer_id': '',\n",
       " 'request_id': '1686987007,50ac2404-d8a9-4a23-8768-44bc978944df',\n",
       " 'time_used': 113}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"LXJnAagsLD_h-Qc1uKEx_qLzNbR4EI2P\",\n",
    "    \"api_secret\":\"lU_3QDEOZlfWTJG0W9V0h3HzVscnTrPD\",\n",
    "    \"faceset_token\":\"32089d204389ecd65fd26a698d8e6b01\",\n",
    "    \"face_tokens\":'5a60d7a287c6d18df8c8815c3cc55d51'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
